package com.pearadmin.pro.modules.laboratoryManagement.controller;

import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.laboratoryManagement.domain.ApplicationRequest;
import com.pearadmin.pro.modules.laboratoryManagement.domain.Applicationtable;
import com.pearadmin.pro.modules.laboratoryManagement.domain.Dangerousroom;
import com.pearadmin.pro.modules.laboratoryManagement.domain.DangerurroomRequest;
import com.pearadmin.pro.modules.laboratoryManagement.service.ApplicationService;
import com.pearadmin.pro.modules.laboratoryManagement.service.LMService;
import com.pearadmin.pro.modules.sys.domain.SysDept;
import com.pearadmin.pro.modules.sys.domain.SysDictData;
import com.pearadmin.pro.modules.sys.service.SysDeptService;
import com.pearadmin.pro.modules.sys.service.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实训室危险品管理
 *
 * @author ZhouXingYun
 * @date 2022/5/18 11:09
 */
@Api(tags = {"实训室危险品管理"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "ac")
public class ApplicationController extends BaseController {

    @Resource
    private ApplicationService applicationService;
    @Resource // 数据字典业务类
    private SysDictDataService sysDictDataService;

    @Resource // 部门 业务类
    private SysDeptService sysDeptService;

    @Resource
    private LMService lmService;

    @Resource // 用户基本信息
    private UserContext userContext;


    /**
     * 查询实训室危险物品列表
     *
     * @param request 查询参数
     */
    @GetMapping("page")
    @Log(title = "查询实训室危险物品列表")
    @ApiOperation(value = "查询实训室危险物品列表")
    public Result page(ApplicationRequest request) {
        PageResponse<Applicationtable> page = applicationService.page(request);
        return success(page);
    }

    /**
     * 危险物品使用申请
     *
     * @param param
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/5/18 16:31
     */
    @PostMapping("apply")
    @Log(title = "危险物品使用申请")
    @ApiOperation(value = "危险物品使用申请")
    public Result apply(@RequestBody Map<String, String> param) {
        // 根据ID查询
        String id = param.get("id");
        Integer number = Integer.valueOf(param.get("number")); //剩余可用数量
        Integer applicationNumber = Integer.valueOf(param.get("applicationNumber")); //需申请数量
        Integer goodssum = Integer.valueOf(param.get("goodssum")); //物品总数
        if (applicationNumber > number) {
            return failure("剩余可用数量不足");
        }
        Applicationtable applicationtable = new Applicationtable();
        applicationtable.setApplication(param.get("remark"));
        applicationtable.setApplicationTime(new Date());
        applicationtable.setApplicationNumber(applicationNumber);
        applicationtable.setDanferousroomId(param.get("id"));
        applicationtable.setStatus(String.valueOf(0));
        applicationtable.setApplicationUser(userContext.getUserId());
        Boolean a = applicationService.apply(applicationtable);
        if(a){
            Dangerousroom dangerousroom = new Dangerousroom();
            dangerousroom.setId(Integer.valueOf(param.get("id")));
            dangerousroom.setNumber(number-applicationNumber);
            lmService.updateById(dangerousroom);
        }
        return auto(a);
    }


    /**
     * 危险物品使用审批
     *
     * @param id
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/5/18 16:31
     */
    @PostMapping("applyAudit")
    @Log(title = "危险物品使用审批成功")
    @ApiOperation(value = "危险物品使用审批成功")
    public Result applyAudit(@RequestParam Integer id) {
        Applicationtable applicationtable = new Applicationtable();
        applicationtable.setId(id);
        applicationtable.setStatus("1");
        return auto(applicationService.updateById(applicationtable));
    }
    /**
     * 危险物品使用审批拒绝
     *
     * @param id
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/5/18 16:31
     */
    @PostMapping("applyRefuse")
    @Log(title = "危险物品使用审批拒绝")
    @ApiOperation(value = "危险物品使用审批拒绝")
    public Result applyRefuse(@RequestParam Integer id) {
        Applicationtable applicationtable = new Applicationtable();
        applicationtable.setId(id);
        applicationtable.setStatus("3");
        return auto(applicationService.updateById(applicationtable));
    }



    /**
     * 危险物品使用归还
     *
     * @param param
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/5/18 16:31
     */
    @PostMapping("applyReturn")
    @Log(title = "危险物品使用归还")
    @ApiOperation(value = "危险物品使用归还")
    public Result applyReturn(@RequestBody Map<String, String> param) {

        if(Integer.valueOf(param.get("applicationNumber"))<Integer.valueOf(param.get("returnNumber"))){
            return failure("您没有申请那么多哦，请认真填写！");
        }

        // 根据ID查询
        Applicationtable applicationtable = new Applicationtable();
        applicationtable.setId(Integer.valueOf(param.get("id")));
        applicationtable.setStatus("2");
        boolean b = applicationService.updateById(applicationtable);
        if(b){
            Dangerousroom danferousroomId = lmService.getById(param.get("danferousroomId"));
            danferousroomId.setNumber(danferousroomId.getNumber()+Integer.valueOf(param.get("returnNumber")));
            boolean b1 = lmService.updateById(danferousroomId);
            return auto(b1);
        }
        return auto(false);
    }

}
